﻿//HTML表单对象扩展2:02 2009-3-17
function _LoadXML(sXMLPath)
{
    if(sXMLPath.indexOf("?")>0)
    {
        sXMLPath+="&rnd="+newGuid();
    }
    else{
        sXMLPath+="?rnd="+newGuid();
    }
	var xml = $.ajax({
			type:"GET",
			url: sXMLPath,
			dataType:"xml",
    		async: false,
			error: function(xml){ 
		        alert("Error loading XML document:"+sXMLPath); 
		    	}
			}).responseXML;
	return xml;
}

/*输入限制*/
function __InputMask(sId,sType,callbackFunc)
{
	switch (sType)
	{
		case "int":
		document.getElementById(sId).onkeyup=function()
		{
			this.value=parseInt(this.value);
			if (isNaN(this.value))
			{
				this.value="";
			}
			else
			{
				if (callbackFunc)
				{
					callbackFunc();
				}
			}
			
		}
		break;
		case "money":case "number":
		document.getElementById(sId).onkeyup=function()
		{
			if(!this.value) return false;
			var strP=/^\d+(\.\d+)?$/;
			var strP=/[^0-9|\.]/;
			this.value=this.value.replace(strP,"");
			if (callbackFunc)
			{
				callbackFunc();
			}
		}
		break;
	}
}


function __radiocheckbox_onclick()
{
	//用checkbox来表现radio，以方便取消
	var i,elems;
	elems=document.getElementsByName(this.name);
	for(i=0;i<elems.length;i++)
	{
		if (this!=elems[i])
			elems[i].checked=false;
	}
	elems=null;
}

function gE(__id)
{
	return document.getElementById(__id);
}


/*表单操作*/
function _form()
{

}
_form.prototype.focusRadio=function(sName,sValue)
{
	var i,j;
	var elems=document.getElementsByName(sName);

	for(i=0;i<elems.length;i++)
	{
		if (elems[i].value.toString()==sValue.toString())
		{
			elems[i].checked=true;
			break;
		}
		else
		{
			if (sValue.toString().toLowerCase()=="false")
			{
				if (elems[i].value.toString()=="0" || elems[i].value.toString().toLowerCase()=="off")
				{
					elems[i].checked=true;
					break;
				}
			}
			if (sValue.toString().toLowerCase()=="true")
			{
				if (elems[i].value.toString().toLowerCase()=="1" || elems[i].value.toString().toLowerCase()=="on")
				{
					elems[i].checked=true;
					break;
				}
			}
		}
	}
	elems=null;
}

_form.prototype.focusCheckbox=function(sName,sValue)
{
	var i,j;
	var focusValue=sValue.split(",");
	var elems=document.getElementsByName(sName);
	for(i=0;i<elems.length;i++)
	{
		for (j=0;j<focusValue.length;j++ )
		{
			//alert("focusCheckbox\n"+elems[i].value.toString()+"\n"+focusValue[j].toString())
			if (elems[i].value.toString()==focusValue[j].toString())
			{
				elems[i].checked=true;
				break;
			}
			/*
			else
			{
				if (focusValue[j].toString().toLowerCase()=="false")
				{
					if (elems[i].value.toString()==focusValue[j].toString())
					{
						elems[i].checked=true;
						break;
					}
				}
			}
			*/
		}
	}
	elems=null;
}

_form.prototype.focusSelect=function(selectId,sValue)
{
	var i;
	var elems=document.getElementById(selectId).options;

	for(i=0;i<elems.length;i++)
	{
		//alert(elems[i].value.toString()+"\n"+sValue.toString());
		if (elems[i].value.toString()==sValue.toString())
		{
			
			elems[i].setAttribute("selected",true);
			break;
		}
	}
	elems=null;
}

//获得radio的值
_form.prototype.getRadioValue=function(sName)
{
	var i,elems,returnValue;
	returnValue="";
	elems=document.getElementsByName(sName);
	if (elems.length==0)
	{
		alert("不存在名称为:"+sName+"的radio对象。")
		return;
	}
	for(i=0;i<elems.length;i++)
	{
		if (elems[i].checked)
		{
			returnValue=elems[i].value;
			break;
		}
	}
	elems=null;
	return returnValue;
}

//获得checkbox的值
_form.prototype.getCheckboxValue=function(sName)
{
	var i,elems,returnValue;
	returnValue="";
	elems=document.getElementsByName(sName);
	if (elems.length==0)
	{
		alert("不存在名称为:"+sName+"的checkbox对象。")
		return "";
	}
	for(i=0;i<elems.length;i++)
	{
		if (elems[i].checked)
		{
			if (returnValue=="")
				returnValue=elems[i].value.toString();
			else
				returnValue=returnValue+","+elems[i].value.toString();
		}
	}
	elems=null;
	return returnValue;
}


_form.prototype.removeOptions=function(formid)
{
	var i;
	var elems=document.getElementById(formid).options;
	while (elems.length>0)
	{
		elems[0].removeNode(true);
	}
	elems=null;
}


_form.prototype.addOption=function(sElemId,sValue,sText)
{
	var elem=document.createElement("option");
	elem.value=sValue;
	elem.textContent=elem.innerText=sText;
	document.getElementById(sElemId).appendChild(elem);
	elem=null;
}

_form.prototype.checkboxReverse = function(sElemName) {

    var elems = document.getElementsByName(sElemName);
    for (i = 0; i < elems.length; i++) {
        elems[i].checked = !elems[i].checked;
    }
    elems = null;
}

_form.prototype.checkboxSelectAll=function(sElemName) {

	var i;
	var elems=document.getElementsByName(sElemName);
	for(i=0;i<elems.length;i++)
	{
		elems[i].checked=true;
	}
	elems=null;
}
_form.prototype.checkboxUnSelectAll=function(sElemName) {

	var i;
	var elems=document.getElementsByName(sElemName);
	for(i=0;i<elems.length;i++)
	{
		elems[i].checked=false;
	}
	elems=null;
}

_form.prototype.checkboxSelect=function(sElemName,value) {
	var i;
	if(!document.getElementsByName(sElemName))
	{
		alert("不存在对象名为["&sElemName&"]的集合.")
		return;
	}
	var elems=document.getElementsByName(sElemName);
	for(i=0;i<elems.length;i++)
	{
		elems[i].checked=value;
	}
	elems=null;
}

_form.prototype.addOptionsByXML=function(sXMLPath,sSelectId,sValue,sText)
{
	var root=_LoadXML(sXMLPath).getElementsByTagName("root")[0];

	var selelem=document.getElementById(sSelectId);
	var elem;
	if (!selelem)
	{
		alert("不存在对象:"+sSelectId);
		return false;
	}
	var v,t;
	for(i=0;i<root.childNodes.length;i++)
	{
		if (root.childNodes[i].nodeType==1)
		{
			v=root.childNodes[i].getElementsByTagName(sValue)[0].text?root.childNodes[i].getElementsByTagName(sValue)[0].text:root.childNodes[i].getElementsByTagName(sValue)[0].textContent;
			t=root.childNodes[i].getElementsByTagName(sText)[0].text?root.childNodes[i].getElementsByTagName(sText)[0].text:root.childNodes[i].getElementsByTagName(sText)[0].textContent;
			
			elem=document.createElement("option");
			elem.value=v;
			elem.innerText=t;
			selelem.appendChild(elem);
			elem=null;
		}
	}
	selelem=null;
	return true;
}

_form.prototype.ElementsDataFromXML=function(formId,url)
{
	var root=_LoadXML(url).getElementsByTagName("root")[0];
	var v,t,index;
    index=0;
	var FormElems=document.getElementById(formId).elements;
	//alert(root.xml);
	for (var i=0;i<FormElems.length;i++)
	{
		if (FormElems[i].tagName=="INPUT")
		{
			try
			{
				
				if (FormElems[i].type=="text" || FormElems[i].type=="hidden")
				{
					v=FormElems[i].name.toLowerCase();
					
					if(v)
					{
						if (root.childNodes[index].getElementsByTagName(v)[0])
						{
						    
							t=root.childNodes[index].getElementsByTagName(v)[0].text?root.childNodes[index].getElementsByTagName(v)[0].text:root.childNodes[index].getElementsByTagName(v)[0].textContent;
							if (t==undefined)
								t="";
							FormElems[i].value=t;
						}
					}
				}
			}
			catch(e)
			{
				alert("text\n节点ID:"+FormElems[i].id+"\n\错误信息:\n"+e.toString());
			}
			try
			{
				if (FormElems[i].type=="radio")
				{
					v=FormElems[i].name.toLowerCase();
					if (root.childNodes[index].getElementsByTagName(v)[0])
					{
						t=root.childNodes[index].getElementsByTagName(v)[0].text?root.childNodes[index].getElementsByTagName(v)[0].text:root.childNodes[index].getElementsByTagName(v)[0].textContent;
						if (t==undefined)
							t="";
						this.focusRadio(FormElems[i].name,t);
					}
				}
			}
			catch(e)
			{
				alert("radio\n节点ID:"+FormElems[i].id+"\n\错误信息:\n"+e.toString());
			}

			try
			{
				if (FormElems[i].type=="checkbox")
				{
					v=FormElems[i].name.toLowerCase();
					if (root.childNodes[index].getElementsByTagName(v)[0])
					{
						t=root.childNodes[index].getElementsByTagName(v)[0].text?root.childNodes[index].getElementsByTagName(v)[0].text:root.childNodes[index].getElementsByTagName(v)[0].textContent;
						if (t==undefined)
							t="";
						this.focusCheckbox(FormElems[i].name,t);					
					}
				}
			}
			catch(e)
			{
				alert("focusCheckbox\n节点ID:"+FormElems[i].id+"\n\错误信息:\n"+e.toString());
				return;
			}
		}
		if (FormElems[i].tagName=="SELECT")
		{
			try
			{
				v=FormElems[i].name.toLowerCase();
				if (root.childNodes[index].getElementsByTagName(v)[0])
				{
					t=root.childNodes[index].getElementsByTagName(v)[0].text?root.childNodes[index].getElementsByTagName(v)[0].text:root.childNodes[index].getElementsByTagName(v)[0].textContent;
					if (t==undefined)
						t="";
					this.focusSelect(FormElems[i].id,t);
				}
			}
			catch(e)
			{
				alert("select\n节点ID:"+FormElems[i].id+"\n\错误信息:\n"+e.toString());
			}
		}
		if (FormElems[i].tagName=="TEXTAREA")
		{
			v=FormElems[i].name.toLowerCase();
			if (root.childNodes[index].getElementsByTagName(v)[0])
			{
				t=root.childNodes[index].getElementsByTagName(v)[0].text?root.childNodes[index].getElementsByTagName(v)[0].text:root.childNodes[index].getElementsByTagName(v)[0].textContent;

				if (t==undefined)
					t="";
				FormElems[i].innerText=t;
			}
		}

	}
	FormElems=null;
	return root;
}

_form.prototype.setdataByUrl = function(formId) {
    var v, t, index;
    index = 0;
    var FormElems = document.getElementById(formId).elements;
    //alert(root.xml);
    for (var i = 0; i < FormElems.length; i++) {
        if (FormElems[i].tagName == "INPUT") {
            try {
                if (FormElems[i].type == "text" || FormElems[i].type == "hidden") {
                    v = FormElems[i].name.toLowerCase();

                    FormElems[i].value = decodeURI(location.toString().QueryString(v));
                }
            }
            catch (e) {
                alert("text\n节点ID:" + FormElems[i].id + "\n\错误信息:\n" + e.toString());
            }
            try {
                if (FormElems[i].type == "radio") {
                    v = FormElems[i].name.toLowerCase();

                    this.focusRadio(FormElems[i].name, decodeURI(location.toString().QueryString(v)));

                }
            }
            catch (e) {
                alert("radio\n节点ID:" + FormElems[i].id + "\n\错误信息:\n" + e.toString());
            }

            try {
                if (FormElems[i].type == "checkbox") {
                    v = FormElems[i].name.toLowerCase();
                    this.focusCheckbox(FormElems[i].name, decodeURI(location.toString().QueryString(v)));
                }
            }
            catch (e) {
                alert("focusCheckbox\n节点ID:" + FormElems[i].id + "\n\错误信息:\n" + e.toString());
                return;
            }
        }
        if (FormElems[i].tagName == "SELECT") {
            
            try {
                v = FormElems[i].name.toLowerCase();
                this.focusSelect(FormElems[i].id, decodeURI(location.toString().QueryString(v)));

            }
            catch (e) {
                alert("select\n节点ID:" + FormElems[i].id + "\n\错误信息:\n" + e.toString());
            }
        }
        if (FormElems[i].tagName == "TEXTAREA") {
            v = FormElems[i].name.toLowerCase();
            FormElems[i].innerText = decodeURI(location.toString().QueryString(v));
        }

    }
    FormElems = null;
}


_form.prototype.reset=function(formId)
{
	//清空表单项目的值
	var FormElems=document.getElementById(formId).elements;
	var elems,j;
	for (var i=0;i<FormElems.length;i++)
	{

		if (FormElems[i].tagName=="INPUT")
		{
			if (FormElems[i].type=="text" || FormElems[i].type=="hidden")
			{
				FormElems[i].value="";
			}
			else if(FormElems[i].type=="radio" || FormElems[i].type=="checkbox")
			{
				var elems=document.getElementsByName(FormElems[i].name);

				for(j=0;j<elems.length;j++)
				{
					elems[j].checked=false;
				}
			}
		}
		if (FormElems[i].tagName=="SELECT")
		{
			this.removeOptions(FormElems[i].id);
		}
		if (FormElems[i].tagName=="TEXTAREA")
		{
			FormElems[i].value="";
		}
	}
	FormElems=null;
}

_form.prototype.isEmail=function(s)
{
	var emailStr=s;
	var emailPat=/^(.+)@(.+)$/;
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null)
	{
		return false;
	}
	return true;
}


$form=new _form();


/*输入限制*/
function __InputMask(sId,sType,callbackFunc)
{
	//var f=this.getAttriubute("firstvalue");
	//if (!f)
	//{
	//	f="";
	//}
	switch (sType)
	{
		case "int":
		document.getElementById(sId).onkeyup=function()
		{
			var strP=/[^0-9]/;
			this.value=this.value.replace(strP,"");

			if (callbackFunc)
			{
				callbackFunc();
			}
		}
		break;
		case "money":case "number":
		document.getElementById(sId).onkeyup=function()
		{
			if(!this.value) return false;
			var strP=/^\d+(\.\d+)?$/;
			var strP=/[^0-9|\.]/;
			this.value=this.value.replace(strP,"");
			if (callbackFunc)
			{
				callbackFunc();
			}
		}
		break;
	}
}


function __radiocheckbox_onclick()
{
	//用checkbox来表现radio，以方便取消
	var i,elems;
	elems=document.getElementsByName(this.name);
	for(i=0;i<elems.length;i++)
	{
		if (this!=elems[i])
			elems[i].checked=false;
	}
	elems=null;
}

function isNumber(oNum)
{
	if(!oNum) return false;
	oNum=oNum.toString();
	var strP=/^-?\d+(\.\d+)?$/;
	if(!strP.test(oNum)) return false;
	try{
		if(parseFloat(oNum)!=oNum) return false;
	}
	catch(ex)
	{
		return false;
	}
	return true;
}

function isDate(DateString , Dilimeter)
{
	if (DateString==null) return false;
		if (Dilimeter=="" || Dilimeter==null)
			Dilimeter = "-";
			
		var tempy="";
		var tempm="";
		var tempd="";
		var tempArray;
		
		if (DateString.length<8 && DateString.length>10)
			return false; 
			
		tempArray = DateString.split(Dilimeter);
		
		if (tempArray.length!=3)
			return false;
			
		if (tempArray[0].length==4)
		{
			tempy = tempArray[0];
			tempd = tempArray[2];
		}
		else
		{
			tempy = tempArray[2];
			tempd = tempArray[1];
		}
		tempm = tempArray[1];
		var tDateString = tempy + "/"+tempm + "/"+tempd+" 8:0:0";	//加八小时是因为我们处于东八区
		var tempDate = new Date(tDateString);
		if (isNaN(tempDate))
			return false;
		if (((tempDate.getUTCFullYear()).toString()==tempy) && (tempDate.getMonth()==parseInt(tempm)-1) && (tempDate.getDate()==parseInt(tempd)))
		{
			return true;
		}
	else
	{
		return false;
	}
}

function isOnlyEnglishAndNumber(sValue)
{
	var usern=/^[a-zA-Z0-9]{1,}$/;
	//if(!sValue.toString.match(usern))
	//{
	//	return false;
	//}
	return true;
}

function getDateDiff(date1,date2,__m)
{
	//日期比较
	var   re   =   /^(\d{4})\S(\d{1,2})\S(\d{1,2})$/;   
	var   dt1,dt2;   
	if   (re.test(date1))   
	{
	    dt1 = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3);
	    alert(dt1);  
	}
	if   (re.test(date2))   
	{
	    dt2 = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3);
	    alert(dt2);
	}

	if (__m="day")
		return Math.floor((dt1-dt2)/(1000 * 60 * 60 * 24));
	else if(__m="hour")
		return Math.floor((dt1-dt2)/(1000 * 60 * 60));
}

function DateDiff(beginDate, endDate) {    //beginDate和endDate都是2007-8-10格式
    var arrbeginDate, Date1, Date2, arrendDate, iDays
    arrbeginDate = beginDate.split("-")
    Date1 = new Date(arrbeginDate[1] + '-' + arrbeginDate[2] + '-' + arrbeginDate[0])    //轉換為2007-8-10格式
    arrendDate = endDate.split("-")
    Date2 = new Date(arrendDate[1] + '-' + arrendDate[2] + '-' + arrendDate[0])
    iDays = parseInt(Math.abs(Date1 - Date2) / 1000 / 60 / 60 / 24)    //轉換為天數 
    return iDays
}    


function newGuid()
{
    var guid = "";
    for (var i = 1; i <= 32; i++){
		var n = Math.floor(Math.random()*16.0).toString(16);
		guid +=   n;
		if((i==8)||(i==12)||(i==16)||(i==20))
			guid += "-";
    }
    return guid.toUpperCase();  
}


/********************/

 

__renderInput=function(){
	
	var i,elems=document.getElementsByTagName("input");
	for (i=0;i<elems.length;i++)
	{
		if (elems[i].getAttribute("type")=="text")
		{

			if (elems[i].getAttribute("datatype"))
			{
				if(elems[i].getAttribute("id"))
				{
					elems[i].setAttribute("firstvalue",elems[i].value);
  					__InputMask(elems[i].getAttribute("id"),elems[i].getAttribute("datatype"));
  				}
  				else
  				{
  					//alert("__renderInput:"+elems[i].outerHTML);
  				}
  			}
  		}
	 }
}

if (document.all) {    
	window.attachEvent('onload', __renderInput);    
}    
else {    
	window.addEventListener('load', __renderInput, false);      
}


